const path = require('path')
const mime = require('mime')

const fs = require('fs')

function myReadFile(file, res) {
   return new Promise((resolve, reject) => {
      fs.readFile(file, (err, data) => {
         if (err) {
            return '你访问的是一个文件夹，且文件夹里面没有html'
         } else {
            resolve(data)
         }
      })
   })
} 

async function readStaticFile(filePathName, res) {
   // let ext=path.parse(filePathName)
   let ext = path.parse(filePathName).ext
   let mimeType = mime.getType(ext)
   let data
   console.log(mimeType)
   // 判断文件是否存在
   console.log(fs.existsSync(filePathName)) 
   if (fs.existsSync(filePathName)) { 

      if (ext) {
        await  myReadFile(filePathName) 
            .then(result => data = result)
            // .catch((err) => data = err) 
      } else {
         await myReadFile(path.join(filePathName, './index.html'), res)
      }
   } 
   else {
      data = 'file not fount';  
   } 
  
   return { mimeType, data }
} 
module.exports = readStaticFile